package com.cpecc.dao.system;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.LDAP;

@Repository
public class LDAPDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@SuppressWarnings("unchecked")
	public LDAP getLDAP(){
		String hql="from LDAP t";
		Session session=sessionFactory.openSession();
		List<LDAP> list=session.createQuery(hql).list();
		session.close();
		if(list.size()>0){
			return list.get(0);			
		}else
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public LDAP getLDAP2(){
		String hql="from LDAP t";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		List<LDAP> list=query.list();
		if(list.size()>0)
			return list.get(0);
		else
			return null;			
	}
	
	public void save(LDAP ldap){
		Session session=sessionFactory.getCurrentSession();
		session.save(ldap);
		session.flush();
	}
	
	public boolean cover(LDAP ldap){
		String hql="update LDAP t set t.url=?,t.adminName=?,t.adminPassword=?,t.searchBase=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, ldap.getUrl());
		query.setString(1, ldap.getAdminName());
		query.setString(2, ldap.getAdminPassword());
		query.setString(3, ldap.getSearchBase());
		query.setString(4, ldap.getId());
		return query.executeUpdate()>0;
	}
}
